<?php

	class IO_Upload
	{
		/*
		* Luu giu message loi chuong trinh
		*/
		public $m_strMsgErr;
		/*
		*	Luu giu path upload hinh
		*/
		public $m_strPathImg;
		/*
		* Luu ten hinh upload
		*/
		public $m_strNameImg;
		/*
		* Luu giu prefix khi tao thumbnal cho hinh
		*/
		private $m_strPrefixThumb ='';
		/*
		*	Luu giu kich thuoc can thumbnail
		*/		
		private $m_intSizeThumb;
		/*
		* Luu giu Width cua hinh thumbnail
		*/
		private $m_intWidthThumb;
		/*
		* Luu giu height cua hinh thumbnail
		*/
		private $m_intHeightThumb;
		/*
		/*
		* Luu giu Width cua hinh source
		*/
		private $m_intWidthSource;
		/*
		* Luu giu height cua hinh source
		*/
		private $m_intHeightSource;
		/*
		*	Luu bien biet he thong dang co dung thu vien gd2 de tao thumbnail hay ko
		*/
		private $m_bolGd2;
		/*
		*	Luu flg de khi tao thumbnail cho hinh theo ti le ngan nhat hay dai nhat
		*/
		public $m_flgThumbFollow = 1;
		/*
			Luu giu phan mo rong cua file can upload
		*/
		private $m_strExtFile = '';
		/*
			Luu ten hinh upload khong co extension cua file
		*/
		private $m_strNameImgNotExt = '';
		/*
		* Hàm contruction
		* Type: Function <br />
		*	Name: upload_class <br />
		*	Modify: Thach Le <lhthach82@yahoo.com.vn> <br />
		*	Create date: Oct 12 08 <br />
		* Descript:
		* Param:  $p_bolGd2: he thong co dung thu viec gd2 cho viec thumbnail hay khong
		*					$p_strDistiPath: thu muc se upload hinh
		*													Neu null la dung cho tao thumbnail truc tiep, khong dung chung 1 object upload hinh
		*						
		*	$p_flgThumbFollow: 1. Tao theo chieu dai nhat, 0: tao theo chieu ngan, 2. Theo chieu ngang, 3. Theo chieu doc			
		*/
		public function IO_Upload($p_bolGd2 = false, $p_strDistiPath = '', $p_flgThumbFollow = 1)
		{
			$this -> m_bolGd2 = $p_bolGd2;
			$this -> m_strPathImg = $p_strDistiPath;
			$this -> m_flgThumbFollow = $p_flgThumbFollow;
		}
		
		/*
		* Hàm set prefix thumbnail khi tao thumbnail cho hinh upload
		* Type: Function <br />
		*	Name: setPrefixThumb <br />
		*	Modify: Thach Le <lhthach82@yahoo.com.vn> <br />
		*	Create date: Oct 12 08 <br />
		* Descript: 
		*/
		public function setPrefixThumb($p_strPrefix = '')
		{
			$this -> m_strPrefixThumb = $p_strPrefix;
		}
		
		/*
		* Hàm tao thumbnail
		* Type: Function <br />
		*	Name: createThumbnail <br />
		*	Modify: Thach Le <lhthach82@yahoo.com.vn> <br />
		*	Create date: Oct 12 08 <br />
		* Descript: Tao thumbnail theo 2 cach
								+ Tao thumbnail tu hinh vua upload su dung cung object
								+ Tao thumbnail tu 1 hinh bat ki da co tren he thong
		*/
		public function createThumbnail($p_intSize, $p_strPathDistiThumb, $p_strNameThumb, $p_strPathSource = '', $p_strNameSource = '')
		{
			// distination path
			if ( substr($p_strPathDistiThumb,-1) == '/' )
				$p_strPathDistiThumb = substr($p_strPathDistiThumb,0,-1);
			$strDistiThumbPathFile = $p_strPathDistiThumb . "/" . $this -> m_strPrefixThumb . $p_strNameThumb;
			
			$this -> m_intSizeThumb = $p_intSize;
			//set source can tao thumbnail
			if ( $p_strPathSource != '' )
				$this -> m_strPathImg = $p_strPathSource;
				
			// strip ki tu / cuoi trong strPathImg
			if ( substr($this -> m_strPathImg,-1) == '/' )
				$this -> m_strPathImg = substr($this -> m_strPathImg,0,-1);
			
			// set Width, Height cua thumbnail
			if ( !$this -> setWidthHeightThumb() )
				return false;
			
			// set ten hinh can tao thumbnail
			if ( $p_strNameSource != '' )  
				$this -> m_strNameImg = $p_strNameSource;
				
			// tao new image tu source
			$fileNewImg = $this -> createImageFromFile();
			if ( !$fileNewImg )
				return false;	
			
			// tai blank cho thumbnail	
			$thumbnailImage = $this->createBlankImage();	
			
			if ($this->m_bolGd2 )
			{
   				imagecopyresampled($thumbnailImage, $fileNewImg, 0, 0, 0, 0, $this -> m_intWidthThumb, $this -> m_intHeightThumb, $this ->m_intWidthSource, $this -> m_intHeightSource); 				 				
			}
			else
			{
				@ImageCopyResized( $thumbnailImage, $fileNewImg, 0, 0, 0, 0, $this -> m_intWidthThumb, $this -> m_intHeightThumb, $this -> m_intWidthSource, $this -> m_intHeightSource ); 
			}
						
			//output image to file
			$error = true;
			if ( !$this->outputImageToFile($thumbnailImage, $strDistiThumbPathFile) )
			{
				$this -> m_strMsgErr = "Can't out put Image blank thumbnail into file";
				$error = false;
			}
			
			chmod($strDistiThumbPathFile, 0755);
			
			@imagedestroy($fileNewImg);
			@imagedestroy($thumbnailImage);
			return $error;
			
			
		}
		
		/*
		* Hàm out put image tao thumbnail toi file
		* Type: Function <br />
		*	Name: createThumbnail <br />
		*	Modify: Thach Le <lhthach82@yahoo.com.vn> <br />
		*	Create date: Oct 12 08 <br />
		*/
		function outputImageToFile($p_thumbnailImage, $p_strPathDisti)
		{
			$pos = strrpos($p_strPathDisti, '.');		//get position of '.' char
			$type = substr($p_strPathDisti, $pos + 1);		//get substr from $pos to the end of path
			$type = strtolower($type); //change all to lower case
			
			switch($type)
			{
				case 'gif':
					return @imagegif($p_thumbnailImage, $p_strPathDisti);
					break;
				case 'jpg' || 'jpeg':
					return @imagejpeg($p_thumbnailImage, $p_strPathDisti);
					break;
				case 'png':
					return @imagepng($p_thumbnailImage, $p_strPathDisti);
					break;
				case 'bmp':
					return @imagewbmp($p_thumbnailImage, $p_strPathDisti);
					break;				
			}
		}
		/*
		* Hàm create blank image from the width and height
		* Type: Function <br />
		*	Name: createThumbnail <br />
		*	Modify: Thach Le <lhthach82@yahoo.com.vn> <br />
		*	Create date: Oct 12 08 <br />
		*/
		function createBlankImage()
		{
			if($this-> m_bolGd2)
			{
				return @imagecreatetruecolor($this -> m_intWidthThumb, $this -> m_intHeightThumb);
			}
			else
			{
				return @imagecreate($this -> m_intWidthThumb, $this -> m_intHeightThumb); 	
			}
		}
		
		/*
		* Hàm create new image from file path
		* Type: Function <br />
		*	Name: createThumbnail <br />
		*	Modify: Thach Le <lhthach82@yahoo.com.vn> <br />
		*	Create date: Oct 12 08 <br />
		*/
		
		function createImageFromFile()
		{
			
			$strPathImgFile = $this -> m_strPathImg . "/" . $this -> m_strNameImg;
						
			$arrInforImg = @getimagesize($strPathImgFile);
			$fileNewImg = false;
			
			switch($arrInforImg[2])	// Determine type
			{
				case 1:		//GIF
					if (function_exists("imagecreatefromgif")) 
					{
						$fileNewImg = @imagecreatefromgif($strPathImgFile);
					}
					else
					{
						//die("Unsupported image type: GIF");
						return false;
					}
					break;
				case 2:		//JPG
					if (function_exists("imagecreatefromjpeg")) 
					{
						$fileNewImg = @imagecreatefromjpeg($strPathImgFile);
					}
					else
					{
						//die("Unsupported image type: JPG");
						return false;
					}
					break;
				case 3:		//PNG
					if (function_exists("imagecreatefrompng")) 
					{
						$fileNewImg = @imagecreatefrompng($strPathImgFile);
					}
					else
					{
						//die("Unsupported image type: PNG");
						return false;
					}
					break;
				case 6:		//BMP
					if (function_exists("imagecreatefrombmp")) 
					{
						$fileNewImg = @imagecreatefromwbmp($strPathImgFile);
					}
					else
					{
						//die("Unsupported image type: BMP");
						return false;
					}
					break;
			}//end switch
			return $fileNewImg;
		}
		
		/*
		* Hàm set width height cua thumbnail
		* Type: Function <br />
		*	Name: setWidthHeightThumb <br />
		*	Modify: Thach Le <lhthach82@yahoo.com.vn> <br />
		*	Create date: Oct 12 08 <br />
		*/
		public function setWidthHeightThumb()
		{
			if ( $this -> m_flgThumbFollow == 0 )
				return $this -> getWidthHeightShort();
			else if ( $this -> m_flgThumbFollow == 1 )
				return $this -> getWidthHeightLong();
			else if ( $this -> m_flgThumbFollow == 2 )
				return $this -> getWidthHeightFollowWidth();
			else if ( $this -> m_flgThumbFollow == 3 )
				return $this -> getWidthHeightFollowHeight();	
		}
		
		function getWidthHeightFollowHeight()
		{
			if( !$arrSize = @getimagesize($this -> m_strPathImg . "/" . $this -> m_strNameImg) )
			{
				$this -> m_strMsgErr = "Can not open image file: " . $this -> m_strPathImg . "/" . $this -> m_strNameImg;
				return false;
			}
			$this -> m_intWidthSource = $arrSize[0];
			$this -> m_intHeightSource = $arrSize[1];
			
			if ( ($this -> m_intWidthSource < $this -> m_intSizeThumb) && ($this -> m_intHeightSource < $this -> m_intSizeThumb) )
			{
				$this -> m_intWidthThumb = $this -> m_intWidthSource;
				$this -> m_intHeightThumb = $this -> m_intHeightSource;
				return true;
			}		
			
			if ( $this -> m_intHeightSource <= $this -> m_intSizeThumb )			
			{
				$this -> m_intWidthThumb = $this -> m_intWidthSource;
				$this -> m_intHeightThumb = $this -> m_intHeightSource;
			}
			else
			{
				$this -> m_intHeightThumb = $this -> m_intSizeThumb;
				$this -> m_intWidthThumb = ($this -> m_intHeightThumb * $this -> m_intWidthSource) / $this -> m_intHeightSource;
			}
						
			return true;
		}
		
		function getWidthHeightFollowWidth()
		{
			if( !$arrSize = @getimagesize($this -> m_strPathImg . "/" . $this -> m_strNameImg) )
			{
				$this -> m_strMsgErr = "Can not open image file: " . $this -> m_strPathImg . "/" . $this -> m_strNameImg;
				return false;
			}
			$this -> m_intWidthSource = $arrSize[0];
			$this -> m_intHeightSource = $arrSize[1];
			
			if ( ($this -> m_intWidthSource < $this -> m_intSizeThumb) && ($this -> m_intHeightSource < $this -> m_intSizeThumb) )
			{
				$this -> m_intWidthThumb = $this -> m_intWidthSource;
				$this -> m_intHeightThumb = $this -> m_intHeightSource;
				return true;
			}		
			
			if ( $this -> m_intWidthSource <= $this -> m_intSizeThumb )			
			{
				$this -> m_intWidthThumb = $this -> m_intWidthSource;
				$this -> m_intHeightThumb = $this -> m_intHeightSource;
			}
			else
			{
				$this -> m_intWidthThumb = $this -> m_intSizeThumb;
				$this -> m_intHeightThumb = ($this -> m_intWidthThumb * $this -> m_intHeightSource) / $this -> m_intWidthSource;
			}
						
			return true;
		}
		
		function getWidthHeightShort()
		{
			if( !$arrSize = @getimagesize($this -> m_strPathImg . "/" . $this -> m_strNameImg) )
			{
				$this -> m_strMsgErr = "Can not open image file: " . $this -> m_strPathImg . "/" . $this -> m_strNameImg;
				return false;
			}
			$this -> m_intWidthSource = $arrSize[0];
			$this -> m_intHeightSource = $arrSize[1];
			
			if ( ($this -> m_intWidthSource < $this -> m_intSizeThumb) && ($this -> m_intHeightSource < $this -> m_intSizeThumb) )
			{
				$this -> m_intWidthThumb = $this -> m_intWidthSource;
				$this -> m_intHeightThumb = $this -> m_intHeightSource;
				return true;
			}		
			
			if ( $this -> m_intWidthSource <= $this -> m_intHeightSource )			
			{
				$this -> m_intWidthThumb = $this -> m_intSizeThumb;
				$this -> m_intHeightThumb = ($this -> m_intHeightSource * $this -> m_intWidthThumb) / $this -> m_intWidthSource;
			}
			else
			{
				$this -> m_intHeightThumb = $this -> m_intSizeThumb;
				$this -> m_intWidthThumb = ($this -> m_intWidthSource * $this -> m_intHeightThumb) / $this -> m_intHeightSource;
			}
						
			return true;
		}
		
		function getWidthHeightLong()
		{
			if( !$arrSize = @getimagesize($this -> m_strPathImg . "/" . $this -> m_strNameImg) )
			{
				$this -> m_strMsgErr = "Can not open image file: " . $this -> m_strPathImg . "/" . $this -> m_strNameImg;
				return false;
			}
			$this -> m_intWidthSource = $arrSize[0];
			$this -> m_intHeightSource = $arrSize[1];
			
			if ( ($this -> m_intWidthSource < $this -> m_intSizeThumb) && ($this -> m_intHeightSource < $this -> m_intSizeThumb) )
			{
				$this -> m_intWidthThumb = $this -> m_intWidthSource;
				$this -> m_intHeightThumb = $this -> m_intHeightSource;
				return true;
			}		
			
			if ( $this -> m_intWidthSource >= $this -> m_intHeightSource )			
			{
				$this -> m_intWidthThumb = $this -> m_intSizeThumb;
				$this -> m_intHeightThumb = ($this -> m_intHeightSource * $this -> m_intWidthThumb) / $this -> m_intWidthSource;
			}
			else
			{
				$this -> m_intHeightThumb = $this -> m_intSizeThumb;
				$this -> m_intWidthThumb = ($this -> m_intWidthSource * $this -> m_intHeightThumb) / $this -> m_intHeightSource;
			}
						
			return true;
		}
		
		
		/*
		* Hàm upload hinh
		* Type: Function <br />
		*	Name: upload <br />
		*	Modify: Thach Le <lhthach82@yahoo.com.vn> <br />
		*	Create date: Oct 12 08 <br />
		* Descript: Upload hinh browse tu client len folder chi dinh
		*						Reference ten hinh duoc upload
		* Param: 	$p_strDistiPath: thu muc can upload hinh den
		*					$r_strNameImg: ten image upload, argument reference
		*/
		public function uploadImage($p_strNameUpload, &$r_strNameImg)
		{
			// khong ton tai thu muc
			if ( !is_dir ( $this -> m_strPathImg ) )
			{
				$this -> m_strMsgErr = "Không thể tạo thư mục: " . $this -> m_strPathImg;
					return false;
			}
			//	
			$r_strNameImg = '';
			if ( is_uploaded_file($_FILES[$p_strNameUpload]['tmp_name']) )
			{
				$this -> getExtFile($_FILES[$p_strNameUpload]['name']);
				$r_strNameImg = $this -> m_strPrefixThumb . $this -> stripSpecChar($this -> m_strNameImgNotExt) . "." . $this -> m_strExtFile;
				$this -> m_strNameImg = $r_strNameImg;
				// check exist images in folder upload
				if ( is_file($this ->m_strPathImg . "/" . $r_strNameImg) )
				{
					$this -> m_strMsgErr = 'Đã tồn tại tên image nay trong danh mục';
					return false;
				}
				
				if ( !copy($_FILES[$p_strNameUpload]['tmp_name'], $this -> m_strPathImg . "/" . $r_strNameImg ) )
				{
					$this -> m_strMsgErr = 'Không thể copy file upload.';
					return false;
				}	
			}
			else
			{
				$this -> m_strMsgErr = "Không upload được hình.";
				return false;
			}
			chmod($this -> m_strPathImg . "/" . $r_strNameImg, 0755);
			return true;
		}
		
		function getExtFile($p_strFileName)
		{
			$this -> m_strExtFile = strtolower(substr($p_strFileName, (strrpos($p_strFileName, '.') + 1)) );
			$this -> m_strNameImgNotExt = (substr($p_strFileName, 0, (strrpos($p_strFileName, '.'))) );
		}
		
		function stripSpecChar($p_strFileName)
		{
			$str = trim($p_strFileName);
			$str = preg_replace('/\s\s+/', ' ', $str);
			$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
			$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
			$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
			$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
			$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
			$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
			$str = preg_replace("/(đ)/", 'd', $str);
			$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
			$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
			$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
			$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
			$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
			$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
			$str = preg_replace("/(Đ)/", 'D', $str);
			
			$strReturn = '';
			for( $i = 0; $i <= strlen($str); $i++ )
			{
				if ( preg_match('/^[a-zA-Z0-9\-]$/i', substr($str, $i, 1)) )
					$strReturn .= substr($str, $i, 1);
				elseif ( substr($str, $i, 1) == ' ' || substr($str, $i, 1) == '_' )
					$strReturn .= '_';
			}
			return ($strReturn);
		}
		
		/*
		* Hàm get thong bao loi chuong trinh
		* Type: Function <br />
		*	Name: getMsgError <br />
		*	Modify: Thach Le <lhthach82@yahoo.com.vn> <br />
		*	Create date: Oct 12 08 <br />
		* Descript: 
		*	Return message loi
		*/
		public function getMsgError()
		{
			return $this -> m_strMsgErr;
		}
		
		
	}

?>